package com.kaikeba.estack.common.jdbc;

import com.google.common.base.CaseFormat;

import java.util.*;

/**
 * 实现key为aa_bb转换为aaBb此种形式的key存储的map
 *
 * @Auth xingwan
 * @Date 2020/9/8 4:04
 **/
public class UnderScoreLinkedHashMap<K extends String, V> extends LinkedHashMap<K, V> {


    public UnderScoreLinkedHashMap(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
    }

    public UnderScoreLinkedHashMap(int initialCapacity) {
        super(initialCapacity);
    }

    public UnderScoreLinkedHashMap() {
    }

    public UnderScoreLinkedHashMap(Map<? extends K, ? extends V> m) {
        super(m);
    }

    public UnderScoreLinkedHashMap(int initialCapacity, float loadFactor, boolean accessOrder) {
        super(initialCapacity, loadFactor, accessOrder);
    }

    @Override
    public boolean containsKey(Object key) {
        if (key instanceof String) {
            return super.containsKey(underScore2lowerCamel((String) key));
        }
        return super.containsKey(key);
    }

    @Override
    public V get(Object key) {
        return super.get(underScore2lowerCamel(Objects.toString(key)));
    }

    @Override
    public V put(K key, V value) {
        return super.put((K) underScore2lowerCamel(key), value);
    }

    @Override
    public V putIfAbsent(K key, V value) {
        return super.putIfAbsent((K) underScore2lowerCamel(key), value);
    }

    private String underScore2lowerCamel(String key) {
        String k = Objects.toString(key);
        if (key == null || !k.contains("_")) {
            return k;
        }
        return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, k);
    }


}
